<?php
/**
 * Created by PhpStorm.
 * User: tjjs
 * Date: 17-7-18
 * Time: 上午10:51
 */

namespace ActionsHttp;

use Data\Order_for_foods_DAO;
use Data\Order_form_DAO;
use Data\Food_DAO;
use Data\Retreat_food;
use Data\Retreat_food_DAO;

class ConsumerRetreatFood extends BaseActionHttp
{
    public function doAction() {
        parent::doAction();

        // 订单id
        $order_form_id = (int)$this->data[ORDER_FORM_ID];
        // 商品id
        $retreat_food_list = $this->data[RETREAT_FOOD_LIST];
        // 订单记录
        $order_form_record = Order_form_DAO::getRecord($order_form_id);
        // 订单发起人
        $created_by = $order_form_record->getCREATED_BY();
//        if ($created_by != $_SESSION[CONSUMER_ID]) {
//            return $this->SendErrMsg("退菜失败");
//        }

        foreach ($retreat_food_list as $food_id => $food_count) {
            // 订单是否已付款
            $order_form_pay_status = $order_form_record->getPAY_STATUS();
            if ($order_form_pay_status != PAY_STATUS_YES) {
                return $this->SendErrMsg("退菜失败，该订单未付款");
            }

            // 该订单是否已入帐
            if ($order_form_record->getIS_ENTRY() == STATE_YES) {
                return $this->SendErrMsg("退菜失败，该订单已入帐");
            }

            if ($order_form_record->getRETREAT_FOOD_STATUS() == RETREAT_FOOD_STATUS_START) {
                return $this->SendErrMsg("退菜失败，已有退菜申请进行中");
            }

            if ($order_form_record->getRETREAT_ORDER_STATUS() == RETREAT_ORDER_STATUS_START) {
                return $this->SendErrMsg("退菜失败，已有退单申请进行中");
            }

            // 商品记录
            $order_food_record = Order_for_foods_DAO::getRecordByFormIdFoodId($order_form_id,$food_id);
            if (!$order_food_record) {
                return $this->SendErrMsg("未点过此商品，不能退款");
            }

            if ($food_count > $order_food_record->getFOOD_COUNT_LEFT()) {
                return $this->SendErrMsg("退菜数量大于所点数量");
            }

            // 商品记录
            $food_record = Food_DAO::getRecord($food_id);
            if (!$food_record) {
                return $this->SendErrMsg("不存在此商品");
            }
        }


        // 建立退菜记录
        try {
            // 事务 开始
            $dbMain = \GatewayWorker\Lib\Db::instance('main');
            $dbMain->beginTrans();

            foreach ($retreat_food_list as $food_id => $food_count) {
                // 商品记录
                $order_food_record = Order_for_foods_DAO::getRecordByFormIdFoodId($order_form_id,$food_id);
                $food_price = $order_food_record->getFOOD_PRICE();
                $lunchbox_price = $order_food_record->getLUNCHBOX_PRICE();
                if ($order_form_record->getIS_TAKE_OUT() == STATE_YES) {
                    $food_price +=$lunchbox_price;
                }
                $food_price = $food_count*$food_price;
                // 更新
                $order_food_record->setRETREAT_STATUS(RETREAT_FOOD_STATUS_START);
                $order_food_record->setRETREAT_COUNT($food_count);
                $order_food_record->setRETREAT_MONEY($food_price);
                Order_for_foods_DAO::update($order_food_record);
            }
            $order_form_record->setRETREAT_FOOD_STATUS(RETREAT_FOOD_STATUS_START);
            Order_form_DAO::update($order_form_record);

            //事务 提交
            $dbMain->commitTrans();
        } catch (\Exception $ex) {
            //出现异常,回滚
            echo "预料外错误~~~~\n";
            echo $ex->getMessage()."\n";
            echo $ex->getTraceAsString()." ~~~ traceString\n";
            echo $ex->getCode()."  ~~code  \n";
            echo $ex->getFile()." ~~ file \n";
            echo $ex->getLine()." ~~ line \n";
            echo $ex->getPrevious() ." ~~ previous \n";
            $dbMain->rollBackTrans();
            // 预料外的错误
            return $this->SendErrMsg("预料外的错误~!");
        }

        // 返回数据
        $this->ResponseDataToCurrentClient();
    }
}